#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <sstream>
#include <string>


//单程路线
//bfs
using namespace std;
const int N = 510;

int n, m;
bool g[N][N];
int stop[N];
int dist[N];
bool state[N];

void bfs()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    queue<int> que;
    que.push(1);
    state[1] = true;

    for (int i = 1; i <= n; i++)
    {
        if (g[1][i] && !state[i])
        {
            dist[i] = 0;
            state[i] = true;
            que.push(i);
        }
    }
    while (que.size())
    {
        int temp = que.front();
        que.pop();

        for (int i = 1; i <= n; i++)
        {
            if (g[temp][i] && !state[i])
            {
                dist[i] = dist[temp] + 1;
                state[i] = true;
                que.push(i);
            }
        }
    }
}

int main()
{

    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    cin >> m >> n;
    string s;
    getline(cin, s);
    for (int i = 0; i < m; i++)
    {
        getline(cin, s);
        stringstream ssin(s);
        int cnt = 0, p;
        while (ssin >> p)
        {
            stop[cnt++] = p;
        }

        for (int j = 0; j < cnt; j++)
        {
            for (int k = j + 1; k < cnt; k++)
            {
                g[stop[j]][stop[k]] = true;
            }
        }
    }

    bfs();

    if (dist[n] == 0x3f3f3f3f)
    {
        cout << "NO" << endl;
    }
    else
    {

        cout << dist[n] << endl;
    }
    return 0;
}